package com.atzzy.ysmall.ysmall_api.OrderController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atzzy.ysmall.ysmall_allService.orderService.OrderService;
import com.atzzy.ysmall.ysmall_api.config.MyPayConfig;
import com.atzzy.ysmall.ysmall_beans.AOrders;
import com.atzzy.ysmall.ysmall_common.vo.R;
import com.github.wxpay.sdk.WXPay;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author JAVAZeng
 * @Date 2022/4/6 16:58
 * @Version 1.0
 */
@RestController
@RequestMapping("OrderService")
@CrossOrigin
public class OrderController {

    @Reference
    private OrderService orderService;

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public R addOrder(@RequestBody AOrders aOrders) {
        R returnR = null;
        try {
            Map<String,String> orderInfo = orderService.addOrder(aOrders.getCids(),aOrders.getOrders());
            String orderId = orderInfo.get("orderId");
            if(orderId!=null)
            {
                //微信支付 申请支付链接
                WXPay wxPay = new WXPay(new MyPayConfig());

                //设置当前订单的信息
                HashMap<String, String> data = new HashMap<>();
                data.put("body", orderInfo.get("productNames"));//商品描述
                data.put("out_trade_no", orderId);//订单编号作为交易号
                data.put("fee_type", "CNY");//支付币种
//                data.put("total_fee", aOrders.getOrders().getActualAmount()*100+"");//支付金额
                data.put("total_fee","1");
                data.put("trade_type", "NATIVE");//交易类型
                data.put("notify_url","http://ysmall.free.idcfengye.com/pay/callBackSuc");//设置支付完成时的回调方法接口

                Map<String, String> resp = wxPay.unifiedOrder(data);
                orderInfo.put("payUrl",resp.get("code_url"));
                returnR = new R(201, "提交订单成功", orderInfo);
                System.out.println(resp);
            }else{
                returnR = new R(501, "订单提交失败", null);
            }
        } catch (SQLException e) {
            returnR = new R(501, "订单提交失败", null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnR;
    }

    @GetMapping("status")
    public R getOrderStatus(@RequestParam("orderId") String orderId){
        R orderById = orderService.getOrderById(orderId);
        return orderById;
    }
    @GetMapping("list")
    public R list(String userId,String status,int pageNum,int limit){
        R r = orderService.listOrders(userId, status, pageNum, limit);
        return r;
    }
}
